package bintree.leetcode;

public class Num572 {
    //root是否包含subroot
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root ==null && subRoot==null){
            return true;
        }
        if (root ==null || subRoot==null){
            return false;
        }
        return isSameTree(root,subRoot)||isSubtree(root.left,subRoot)||isSubtree(root.right,subRoot);
    }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p==null && q==null){
            return true;
        }
        //有一颗数为空，另一颗不为空
        if (p==null&&q==null){
            return false;
        }
        if (p==null || q==null){
            return false;
        }
        //p和Q不为空，判断一下节点值，子树判断交给子函数
        return p.val == q.val&&isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);

    }
}
